package com.android.systemui.ios.views;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.systemui.ios.R;

public class ShortcutMusicView extends LinearLayout implements View.OnClickListener {

    private final String LOCK_MUSIC_PLAYER_BTN = "lockMusicPlayer";
    private final int MUSIC_PLAYER_BTN_PREV = 1;
    private final int MUSIC_PLAYER_BTN_PLAY = 2;
    private final int MUSIC_PLAYER_BTN_NEXT = 3;

    private TextView mMusicName;
    private ImageView mMusicBtnPrevious;
    private ImageView mMusicBtnPlay;
    private ImageView mMusicBtnNext;

    public ShortcutMusicView(Context context) {
        this(context, null, 0);
    }

    public ShortcutMusicView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShortcutMusicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mMusicName = (TextView) findViewById(R.id.ip_shortcut_music_file_name);
        mMusicBtnPrevious = (ImageView) findViewById(R.id.ip_shortcut_music_control_previous);
        mMusicBtnPlay = (ImageView) findViewById(R.id.ip_shortcut_music_control_play);
        mMusicBtnNext = (ImageView) findViewById(R.id.ip_shortcut_music_control_next);

        mMusicBtnPrevious.setOnClickListener(this);
        mMusicBtnPlay.setOnClickListener(this);
        mMusicBtnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent("android.media.LOCK_MUSIC_PLAYER");
        final int id = v.getId();
        if (R.id.ip_shortcut_music_control_previous == id) {
            intent.putExtra(LOCK_MUSIC_PLAYER_BTN, MUSIC_PLAYER_BTN_PREV);
            getContext().sendBroadcast(intent);
        } else if (R.id.ip_shortcut_music_control_play == id) {
            intent.putExtra(LOCK_MUSIC_PLAYER_BTN, MUSIC_PLAYER_BTN_PLAY);
            getContext().sendBroadcast(intent);
        } else if (R.id.ip_shortcut_music_control_next == id) {
            intent.putExtra(LOCK_MUSIC_PLAYER_BTN, MUSIC_PLAYER_BTN_NEXT);
            getContext().sendBroadcast(intent);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.android.intent.action.MUSIC_PLAYER_INFO");
        getContext().registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        getContext().unregisterReceiver(mReceiver);
    }

    private void updateMusicPlayerInfo(boolean isPlaying, String musicName) {
        if (isPlaying) {
            mMusicBtnPlay.setImageResource(R.drawable.ip_control_music_pause);
        } else {
            mMusicBtnPlay.setImageResource(R.drawable.ip_control_music_play);
        }
        if (musicName != null && !"".equals(musicName)) {
            mMusicName.setText(musicName);
        } else {
            mMusicName.setText(R.string.ip_music_default_file_name);
        }
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if ("com.android.intent.action.MUSIC_PLAYER_INFO".equals(action)) {
                boolean isPlaying = intent.getBooleanExtra("isPlaying", false);
                String artistName = intent.getStringExtra("music_artistName");
                String trackinfoName = intent.getStringExtra("music_trackinfoName");
                String albumName = intent.getStringExtra("music_albumName");
                long totalTime = intent.getLongExtra("music_duration", 0);
                long currentTime = intent.getLongExtra("music_position", 0);
                updateMusicPlayerInfo(isPlaying, artistName);
            }
        }
    };
}
